package py.com.alfa.escriba.web.services.impl;

import java.util.List;

import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DuplicateKeyException;

import py.com.alfa.escriba.exception.ErrorCode;
import py.com.alfa.escriba.exception.ServiceException;
import py.com.alfa.escriba.persistence.dao.PersonasjuridicasinfoDAO;
import py.com.alfa.escriba.persistence.dto.PersonasjuridicasinfoDTO;
import py.com.alfa.escriba.util.Utils;
import py.com.alfa.escriba.web.interceptor.InvocationContext;
import py.com.alfa.escriba.web.services.PersonasjuridicasinfoService;

public class PersonasjuridicasinfoServiceImpl implements PersonasjuridicasinfoService{

  private PersonasjuridicasinfoDAO personasJuridicasInfoDAO;
  


  public void setPersonasJuridicasInfoDAO(
		PersonasjuridicasinfoDAO personasJuridicasInfoDAO) {
	this.personasJuridicasInfoDAO = personasJuridicasInfoDAO;
}

@Override
  public List<PersonasjuridicasinfoDTO> listPersonasjuridicasinfo(InvocationContext ic) throws ServiceException {
    List<PersonasjuridicasinfoDTO> personasjuridicasinfos =  personasJuridicasInfoDAO.listPersonaJuridica();

    return personasjuridicasinfos;
  }

  @Override
  public PersonasjuridicasinfoDTO getPersonasjuridicasinfoById(Long id, InvocationContext ic) throws ServiceException {
    if (id == null || id <= 0)
      throw new ServiceException(ErrorCode.PARAMETER_ERROR,"parameter.error.null","id");

    PersonasjuridicasinfoDTO personasjuridicasinfo =  personasJuridicasInfoDAO.getPersonaJuridicaById(id);

    if (personasjuridicasinfo == null)
      throw new ServiceException(ErrorCode.NO_DATA_FOUND,"personasjuridicasinfo.notfound", id);

    return personasjuridicasinfo;
  }

  @Override
  public void deletePersonasjuridicasinfo(Long id, InvocationContext ic) throws ServiceException {
    if (id == null || id <= 0)
      throw new ServiceException(ErrorCode.PARAMETER_ERROR,"parameter.error.null","id");

    PersonasjuridicasinfoDTO personasjuridicasinfo =  personasJuridicasInfoDAO.getPersonaJuridicaById(id);

    if(personasjuridicasinfo == null)
      throw new ServiceException(ErrorCode.NO_DATA_FOUND,"personasjuridicasinfo.notfound", id);

    try {
    	personasJuridicasInfoDAO.deletePersonaJuridica(personasjuridicasinfo);
    }
    catch (DataIntegrityViolationException e){
      throw new ServiceException(ErrorCode.DATA_INTEGRITY_VIOLATION, "personasjuridicasinfo.constraint.violation", id);
    }

  }

  @Override
  public PersonasjuridicasinfoDTO updatePersonasjuridicasinfo(Long id, PersonasjuridicasinfoDTO dto, InvocationContext ic) throws ServiceException {
    if (id == null || id <= 0)
      throw new ServiceException(ErrorCode.PARAMETER_ERROR,"parameter.error.null","id");

    if (null == dto)
      throw new ServiceException(ErrorCode.PARAMETER_ERROR,"parameter.error.null","dto");

    PersonasjuridicasinfoDTO personasjuridicasinfo = personasJuridicasInfoDAO.getPersonaJuridicaById(id);

    if (personasjuridicasinfo == null)
      throw new ServiceException(ErrorCode.NO_DATA_FOUND,"personasjuridicasinfo.notfound", id);

    personasjuridicasinfo.setIdpersona(dto.getIdpersona());
    personasjuridicasinfo.setTexto(dto.getTexto());
    personasjuridicasinfo.setNumeroinscripcion(dto.getNumeroinscripcion());
    personasjuridicasinfo.setActaconstitucion(dto.getActaconstitucion());
    personasjuridicasinfo.setPersoneria(dto.getPersoneria());
 

    Utils.validarDTO(dto);

    personasJuridicasInfoDAO.updatePersonaJuridica(dto);

    return personasjuridicasinfo;
  }

  @Override
  public PersonasjuridicasinfoDTO createPersonasjuridicasinfo(PersonasjuridicasinfoDTO dto, InvocationContext ic) throws ServiceException {
    if (dto == null)
      throw new ServiceException(ErrorCode.PARAMETER_ERROR,"parameter.error.null","dto");

    try {

      Utils.validarDTO(dto);

      personasJuridicasInfoDAO.createPersonaJuridica(dto);
    }
    catch (DuplicateKeyException e){
      throw new ServiceException(ErrorCode.DUPLICATE_KEY_EXCEPTION, "personasjuridicasinfo.duplicate.key");
    }

    return dto;
  }

}